package kd.bos.openapi.demo.webservice;

import kd.bos.openapi.demo.auth.signauth.SignAuthTest;
import kd.bos.openapi.sdk.util.PropertyUtil;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMXMLBuilderFactory;
import org.apache.axiom.om.OMXMLParserWrapper;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.databinding.utils.BeanUtil;
import org.apache.axis2.util.StreamWrapper;

import javax.xml.namespace.QName;
import java.util.logging.Logger;

/**
 * @author johnz
 * @Deacription 通过axis2框架调用OpenAPI接口
 * @date 2023年10月20日
 */
public class FindOrderCallByAxisDemo {
    private final static Logger log = Logger.getLogger(FindOrderCallByAxisDemo.class.getName());
    // 苍穹OpenAPI调用接口的URL前缀
    public static final String urlPrefix = PropertyUtil.getProperty("bos.env.cosmic");
    // 基本认证参数前缀
    public static final String OPEN_API_SIGN = "/$/openApiSign/";
    // 基本认证secret key
    public static final String OPEN_API_SIGN_SECRET_KEY = PropertyUtil.getProperty("digestAuth.openApiSign");


    public static void main(String[] args) throws AxisFault {
        findOrderByAxis2();
    }

    // 查询订单
    private static void findOrderByAxis2() throws AxisFault {
        log.info("------------------------findOrderByAxis2 start");
        // 接口地址
        String soapBindingAddress = urlPrefix + "kapi/v2/kdtest/open/openapi_unittest/findOrders" + OPEN_API_SIGN + OPEN_API_SIGN_SECRET_KEY;
        ServiceClient sender = new ServiceClient();
        EndpointReference endpointReference = new EndpointReference(soapBindingAddress);
        Options options = new Options();
        options.setTo(endpointReference);
        sender.setOptions(options);

        // 构建请求body
        OMElement soapenv = createBodyData();
        log.info("------------------------request------------------------");
        log.info(soapenv.toString());
        // 发送数据，返回结果
        OMElement result = sender.sendReceive(soapenv);
        log.info("------------------------response------------------------");
        log.info(result.toString());
        log.info("------------------------findOrderByAxis2 end");
    }

	// 构建请求body
    private static OMElement createBodyData() {
		// 接口webservice信息命名空间
        String namespace = "test.openapi.kingdee.com";
        OMFactory fac = OMAbstractFactory.getOMFactory();
        // 这个和qname差不多，设置命名空间
        OMNamespace omNs = fac.createOMNamespace(namespace, "");
        OMElement soapenv = fac.createOMElement("soapenv", omNs);

		// 接口请求入参模型
        data data = new data();
        data.setBillno("003");

        javax.xml.stream.XMLStreamReader reader = BeanUtil.getPullParser(data);
        StreamWrapper parser = new StreamWrapper(reader);
        OMXMLParserWrapper stAXOMBuilder = OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), parser);
        OMElement element = stAXOMBuilder.getDocumentElement();
        soapenv.addChild(element);
		// 分页
        OMElement pageNo = fac.createOMElement(new QName("pageNo"));
        pageNo.setText("1");
        soapenv.addChild(pageNo);
		// 分页页数
        OMElement pageSize = fac.createOMElement(new QName("pageSize"));
        pageSize.setText("10");
        soapenv.addChild(pageSize);
        return soapenv;
    }

}